package com.xy.novelmanagement.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.xy.model.Comment;
import com.xy.model.CommentRules;
import com.xy.model.PageList;
import com.xy.novelmanagement.mapper.CommentRuleManagementMapper;
import com.xy.novelmanagement.service.ICommentRuleManagementService;
import com.xy.utils.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CommentRuleManagementServiceImpl implements ICommentRuleManagementService {
		private Jedis jedis;
		private final ObjectMapper objectMapper = new ObjectMapper();
		@Autowired
		private CommentRuleManagementMapper ruleMapper;
		@Override
		@Transactional
		public Boolean addRule(Map<String, Object> map) {
				boolean result = false;
				jedis = JedisUtil.getConnect();
				try {
						String ruleName = (String) map.get("ruleName");
						String[] ruleList = (String[]) map.get("ruleList");
						if (StringUtils.isEmpty(ruleName)){
								log.info("添加的ruleName为{}",ruleName);
								return false;
						}else if (ruleList.length==0||ruleList==null){
								log.info("添加的ruleList为{}",ruleList);
								return false;
						}
						log.info("ruleList="+ruleList);
						result = ruleMapper.addRule(ruleName, Arrays.toString(ruleList));
						jedis.setex("ruleSing",120,"false");
				} catch (Exception e) {
						log.error("错误消息：{}", e.getMessage(), e);
						return false;
				}finally {
						JedisUtil.close(jedis);
				}
				return result;
		}

		@Override
		public PageList<CommentRules> getList(Integer pageNum, Integer pageSize) {
				PageHelper.startPage(pageNum,pageSize);
				List<CommentRules> rulesList = null;
				jedis = JedisUtil.getConnect();
				PageList<CommentRules> pageList=null;
				try {
						String value = jedis.get("ruleSing")==null?"":jedis.get("ruleSing");
						if (value.equals("true")){
								String values = jedis.get("ruleList");
								pageList = JSONObject.parseObject(values,PageList.class);
						}else {
								rulesList = ruleMapper.getAllRules();
								pageList = new PageList<>(rulesList);
								jedis.setex("ruleSing",120,"true");
								jedis.setex("ruleList",120,objectMapper.writeValueAsString(pageList));
						}
				} catch (Exception e) {
						log.error("错误消息：{}", e.getMessage(), e);
				}finally {
						JedisUtil.close(jedis);
				}
				log.info("pageList = "+pageList);
				return pageList;
		}

		@Override
		@Transactional
		public Boolean deleteRulesByName(String[] delRuleNames) {
				boolean result = false;
				List<String> delList = new ArrayList<>();
				for (int i=0;i<delRuleNames.length;i++){
						if(StringUtils.isEmpty(delRuleNames[i])){
								log.info("CommentRuleManagementService deleteRulesByName delRuleNames[{}] is empty! ",i);
						}else {
								delList.add(delRuleNames[i]);
						}
				}
				try {
						ruleMapper.deleteRules(delList);
						result = true;
						jedis.setex("ruleSing",120,"false");
				}catch (Exception e){
						log.error("错误消息：{}", e.getMessage(), e);
				}
				return result;
		}

		@Override
		@Transactional
		public Boolean updateRule(Map<String, Object> map) {
				if (map.values().size()==0){
						return false;
				}
				Boolean result = false;
				String oldName = (String) map.get("oldRuleName");
				String newRuleName = (String) map.get("newRuleName");
				String[] ruleList = (String[]) map.get("ruleList");
				if (oldName!=null&&!oldName.equals("")){
						if(oldName.equals(newRuleName)){
								newRuleName="";
						}
						log.info("newName = " +newRuleName);
						try {
								result = ruleMapper.update(newRuleName, oldName,Arrays.toString(ruleList));
								jedis.setex("ruleSing",120,"false");
						} catch (Exception e) {
								log.error("错误消息：{}", e.getMessage(), e);
						}
				}
				return result;
		}
}
